package org.ru.mse10.cvis.upload;

import org.ru.mse10.cvis.constants.UserRole;
import org.ru.mse10.cvis.entity.cv.User;
import org.ru.mse10.cvis.util.CommonUtil;

/**
 * Common handler.
 * 
 * @param <T>
 *            the generic type
 * @author Spawn
 */
public class CommonHandler<T> {
	/**
	 * Method that will be override by other handlers.
	 * 
	 * @param row
	 *            - new row/new user read from file.
	 */
	public void handle(T row) {

	}

	/**
	 * Method to set new password to current user.
	 * 
	 * @param user
	 *            - current new user
	 * @return the string
	 */
	protected String setPasswordToUser(User user) {
		String password = CommonUtil.generatePassword();
		user.setPassword(CommonUtil.hashPassword(password));
		return password;
	}

	/**
	 * Method to get role for new created user from file.
	 * 
	 * @param string
	 *            - current role
	 * @return - the role
	 */
	protected UserRole getRole(String string) {
		if (CommonUtil.isEmpty(string)) {
			throw new IllegalArgumentException("Unparsable user role: "
					+ string);
		}
		for (UserRole role : UserRole.values()) {
			if (role.name().equalsIgnoreCase(string)) {
				if (UserRole.INITIAL == role) {
					throw new IllegalArgumentException(
							"Only one user role Initial is allowed in the system.");
				}
				return role;
			}
		}
		throw new IllegalArgumentException("Unparsable user role: " + string);
	}
}
